package cn.iocoder.yudao.module.dify.controller.admin.batchUploadKnowledgeBase;

import cn.hutool.core.io.FileUtil;
import cn.iocoder.yudao.module.dify.service.BatchUploadKnowledgeBase.BatchUploadKnowledgeBaseService;
import cn.iocoder.yudao.module.dify.service.BatchUploadKnowledgeBase.PDFProcessService;
import io.github.imfangs.dify.client.exception.DifyApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/api/dify")
@RequiredArgsConstructor
@Tag(name = "批量上传知识库")
public class BatchUploadKnowledgeBaseController {

    @Resource
    private BatchUploadKnowledgeBaseService batchUploadKnowledgeBaseService;

    @Resource
    private PDFProcessService pdfProcessService;

    @PostMapping(path = "/batchUpload/knowledgeBase")
    @Operation(summary = "批量上传知识库")
    public Boolean batchUploadKnowledgeBase() throws DifyApiException, IOException, InterruptedException {
        return batchUploadKnowledgeBaseService.folderBatchUpload();
    }

    @PostMapping(path = "/batchUpload/test1")
    @Operation(summary = "批量上传知识库")
    public Boolean test001() {
        String pdfPath = "D:\\puyuanData\\法规问答\\YYT1437(idt 24971-2020)医疗器械 风险管理对医疗器械的应用指南（征求意见稿）.pdf";
        String normalize = FileUtil.normalize(pdfPath);
        File file = new File(normalize);
        return pdfProcessService.processPDF(file).isSuccess();
    }

    @PostMapping(path = "/batchUpload/testOCR")
    @Operation(summary = "测试OCR")
    public Boolean testOCR() {
        String pdfPath = "D:\\puyuanData\\翻译测试用例\\一次性使用电子宫腔成像导管初始污染菌报告.pdf";
        String normalize = FileUtil.normalize(pdfPath);
        File file = new File(normalize);
        return pdfProcessService.processPDF(file).isSuccess();
    }

}
